package ui;

import gameObject.Fettle;
import gameObject.Grasp;
import gameObject.Wife;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel{
    BufferedImage bj;
    Wife wife = new Wife();//建立老婆对象
    Grasp grasp = new Grasp(wife);//建立抓手对象
    Fettle fettle = new Fettle(grasp,wife);//建立状态栏对象
    GameFrame frame;
    public GamePanel(GameFrame frame){
        this.frame = frame;
        bj = ImageUtil.getImage("/Images/游戏背景.png");
        //调用键盘监听的方法
        keyListener();
    }
    //画板
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画出游戏背景
        g.drawImage(bj,0,0,null);
        fettle.drawFet(g);
        //画出对象
        wife.drawLoli(g);
        grasp.Drawing(g);
        collDetect(g,grasp,wife);
    }
    //键盘监听
    public void keyListener(){
        KeyAdapter adapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                grasp.addKeyListener(e);
            }
        };
        frame.addKeyListener(adapter); //为游戏窗口注册监听
    }
    //碰撞检测的方法
    public void collDetect(Graphics g, Grasp grasp, Wife wife){
        boolean collide = grasp.getRect().intersects(wife.getRect());
        if (collide){
            wife.setStop(true);
            wife.setX(grasp.getX());
            wife.setY(grasp.getY());
        }
    }
}
